import java.util.Objects;


public class Leetcode0105 {
    public static void main(String[] args) {
        System.out.println(oneEditAway("islander", "slander"));
    }

    public static boolean oneEditAway(String first, String second) {
        if (Objects.equals(first, second)) {
            return true;
        }

        if (first == null || second == null) {
            return false;
        }

        if (first.length() == second.length()) {
            return oneEditReplace(first, second);
        }

        if (first.length() == second.length() + 1 || first.length() == second.length() - 1) {
            return oneAdd(first, second);
        }

        return false;
    }


    private static boolean oneEditReplace(String first, String second) {
        boolean foundDifference = false;
        for (int i = 0; i < first.length(); i++) {
            if (first.charAt(i) != second.charAt(i)) {
                if (foundDifference) {
                    return false;
                }

                foundDifference = true;
            }
        }
        return true;
    }

    private static boolean oneAdd(String first, String second) {
        if (first.length() > second.length()) {
            return oneAdd(second, first);
        }

        int index1 = 0;
        int index2 = 0;
        while (index2 < second.length() && index1 < first.length()) {
            if (first.charAt(index1) != second.charAt(index2)) {
                if (index1 != index2) {
                    return false;
                }

            } else {
                index1++;
            }

            index2++;
        }
        return true;
    }
}

